<?php
/**
 * 易优CMS
 * ============================================================================
 * 版权所有 2016-2028 海南赞赞网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.eyoucms.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 小虎哥 <1105415366@qq.com>
 * Date: 2018-4-3
 */

namespace think\template\taglib\api;

use think\Db;
use think\Request;

/**
 * 会员相关内容
 */
class TagUsers extends Base
{
    //初始化
    protected function _initialize()
    {
        parent::_initialize();
    }

    //获取会员余额列表
    public function getUsersPriceList($users = [], $page = 1, $pagesize = 0,$type='all')
    {
        $condition = [];
        $condition['a.users_id'] = $users['users_id'];
        if ('income' == $type){
            $condition['a.cause_type'] = ['in',[1, 2, 4]];
        }elseif ('expend' == $type ){
            $condition['a.cause_type'] = ['in',[0, 3, 5]];
        }
        $condition['a.status'] = ['in',[2,3]];
        $paginate = array(
            'page' => $page,
        );

        $pages = Db::name('users_money')
            ->alias('a')
//            ->field('a.*,b.user_name,c.nickname')
            ->where($condition)
//            ->join('admin b', 'a.admin_id = b.admin_id', 'left')
//            ->join('users c', 'c.users_id = a.users_id', 'left')
            ->order('a.moneyid desc')
            ->paginate($pagesize, false, $paginate);
        $list = $pages->toArray();

        $pay_status_arr = config('global.pay_cause_type_arr');
        foreach ($list['data'] as $k => $v) {
            if (0 == $v['cause_type']) {
                $v['cause'] = unserialize($v['cause']);
            }
            $v['type'] = 0; //0-支出 1-收入
            //收入
            if (in_array($v['cause_type'], [1, 2, 4])) {
                $v['type'] = 1;
            }

            $v['typename'] = $pay_status_arr[$v['cause_type']];
            $v['add_time'] = $this->time_format($v['add_time']);
            $list['data'][$k] = $v;

        }

        return $list;
    }

    //获取会员积分列表
    public function getUsersScoreList($users = [], $page = 1, $pagesize = 0,$type=0,$parse = [])
    {
        $condition = [];
        $condition['a.users_id'] = $users['users_id'];
        if (!empty($type)){
            $condition['a.type'] = $type;
        }

        if (!empty($parse['date'])){
            $now_month = date($parse['date']);
            $next_month = date('Y-m',strtotime($now_month.' +1 month'));
        }else{
            $now_month = date('Y-m');
            $next_month = date('Y-m',strtotime('next month'));
        }

        $now_month = strtotime($now_month);
        $next_month = strtotime($next_month)-1;
        $condition['a.add_time'] = ['BETWEEN',[$now_month,$next_month]];

        $paginate = array(
            'page' => $page,
        );

        $pages = Db::name('users_score')
            ->alias('a')
            ->where($condition)
            ->order('a.add_time desc')
            ->paginate($pagesize, false, $paginate);
        $list = $pages->toArray();

        $users_score_type_arr = config('global.users_score_type_arr');
        foreach ($list['data'] as $k => $v) {
            $v['typename'] = $users_score_type_arr[$v['type']];
            $v['add_time'] = $this->time_format($v['add_time']);
            $list['data'][$k] = $v;
        }
        $list['type_list'][] = ['val'=>0,'typename'=>'全部'];
        foreach ($users_score_type_arr as $k => $v){
            $arr['typename'] = $v;
            $arr['val'] = $k;
            $list['type_list'][] = $arr;
        }
        if (empty($parse['date'])){
            $list['date'] = date('Y-m');
        }else{
            $list['date'] = $parse['date'];
        }

        return $list;
    }
}